package com.example.battle.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.Target;
import com.example.battle.R;
import com.example.battle.activity.ChangePwd;
import com.example.battle.activity.FeedbackActivity;
import com.example.battle.activity.Login;
import com.example.battle.activity.PersonInfo;
import com.example.battle.activity.SettingsActivity;
import com.example.battle.db.BattleDao;
import com.example.battle.entity.User;
import com.example.battle.util.ActivityCollector;
import com.example.battle.util.PhotoUtils;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

import static android.content.Intent.getIntent;

public class PersonFragment extends Fragment implements View.OnClickListener   {
    private ImageView hBack;
    private ImageView hHead;
    private TextView user_name,user_id,info,changepassword,setting,feedback;
    private User user;
    private BattleDao battleDao;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_person, container, false);
        // 设置背景磨砂效果
        hHead = root.findViewById(R.id.h_head);
        hBack = root.findViewById(R.id.h_back);
//        user_name = root.findViewById(R.id.user_name);
//        user_id = root.findViewById(R.id.user_id);
        info = root.findViewById(R.id.user_info);
        changepassword = root.findViewById(R.id.user_changepassword);
        setting = root.findViewById(R.id.user_setting);
        feedback = root.findViewById(R.id.user_feedback);

        Intent intent = getActivity().getIntent();
        user = (User)intent.getSerializableExtra("user");
//        user_id.setText(user.getUserId());
//        user_name.setText(user.getUserName());

        if (user.getPortrait() != null){
            hBack.setImageBitmap((new PhotoUtils()).byte2bitmap(user.getPortrait()));
            hHead.setImageBitmap((new PhotoUtils()).byte2bitmap(user.getPortrait()));
        }else {
            // 设置磨砂背景
            Glide.with(getActivity()).load(R.drawable.head)
                    .bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity()))
                    .into(hBack);
            //设置圆形图像
            Glide.with(getActivity()).load(R.drawable.head)
                    .bitmapTransform(new CropCircleTransformation(getActivity()))
                    .into(hHead);
        }

        info.setOnClickListener(this);
        changepassword.setOnClickListener(this);
        setting.setOnClickListener(this);
        feedback.setOnClickListener(this);
        return root;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_info:
                // 进入个人信息修改界面
                user = Login.getInstance().getAppDB().battleDao().login(user.getUserId());
                Intent intent1 = new Intent(getActivity(),PersonInfo.class);
                intent1.putExtra("user",user);
                startActivity(intent1);
                break;
            case R.id.user_changepassword:
                // 进入修改密码界面
                Intent intent2 = new Intent(getActivity(), ChangePwd.class);
                intent2.putExtra("user",user);
                startActivity(intent2);
                break;
            case R.id.user_setting:
                // 进入设置界面
                Intent intent3 = new Intent(getActivity(), SettingsActivity.class);
                startActivity(intent3);
                break;
            case R.id.user_feedback:
                // 进入反馈界面
                Intent intent4 = new Intent(getActivity(), FeedbackActivity.class);
                intent4.putExtra("user",user);
                startActivity(intent4);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(getActivity());
    }

}
